<template>
  <div class="contract-list-area">
    <div class="top-fixed">
      <van-nav-bar
        title="客户"
        size="18"
        left-text=""
        right-text=""
        left-arrow
        @click-left="onClickLeft"
        @click-right="onClickRight"
      >
        <template #right>
          <span class="add-btn" @click="onClickRight">
            <img
              src="../assets/image/contractList/add-top.png"
              style="width: 16px; height: autp"
            />
          </span>
        </template>
      </van-nav-bar>
      <div class="op-area">
        <div class="op-left">
          <div class="set-lf">
            <span @click="handleShowSearch"
              >排序<span
                class="icon iconfont"
                style="color: #999; font-size: 12px"
                >&#xe617;</span
              ></span
            >
          </div>
        </div>
        <div class="op-right">
          <div class="set-lf" @click="handleShowPop">
            <span
              >筛选<span
                class="icon iconfont"
                style="color: #999; font-size: 12px; margin-left: 2px"
                >&#xe61e;</span
              ></span
            >
          </div>
        </div>
      </div>
    </div>
    <div class="list-contract">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多啦"
        @load="load_more"
        class="publist"
      >
        <ul v-if="list && list.length">
          <li v-for="item in list" :key="item.id">
            <div class="list-content-box">
              <div class="hd">
                <div class="hd-lf">
                  <i class="hd-bor"></i>
                  <h2 @click="handleGoDetail($event, item)">
                    {{ item.companyName }}
                  </h2>
                </div>
                <div class="hd-rt">
                  <span class="status doing">{{
                    item.followUpStage | formatFollowUpStage
                  }}</span>
                </div>
              </div>
              <van-divider style="margin: 0" />
              <div class="bd">
                <div class="inner-bd">
                  <div class="bd-lf">
                    <p class="row-item">
                      <span>创建时间：</span
                      ><span>{{ parseTime(item.createTime) }}</span>
                    </p>
                    <p class="row-item">
                      <span>客户星级：</span>
                      <van-rate :value="item.customerRating" color="#3875f6" />
                      <!-- <van-rate v-model="rate" color="#3875f6" /> -->
                    </p>
                  </div>
                  <div class="bd-rt">
                    <div class="img-txt">
                      <div class="img-area">
                        <img
                          src="../assets/image/contractList/m.png"
                          alt=""
                          style="height: 16px"
                        />
                      </div>
                      <p class="name">
                        {{ getFirstContactList(item, "contactName") }}
                      </p>
                    </div>
                  </div>
                </div>
              </div>
              <van-divider style="margin: 0" />
              <div class="bottom">
                <span class="b-l1"
                  >跟进记录：
                  <dict-tag
                    :type="DICT_TYPE.BPM_PROCESS_INSTANCE_STATUS"
                    :value="item.status"
                /></span>
                <span class="b-l2"
                  ><span class="list-inner-inf"
                    >电话：{{
                      getFirstContactList(item, "contactMobile")
                    }}</span
                  ></span
                >
              </div>
            </div>
          </li>
        </ul>
      </van-list>
    </div>
    <!-- 筛选 -->
    <van-popup
      v-model="showSecPop"
      round
      closeable
      position="bottom"
      :style="{ height: '90%' }"
    >
      <div class="pop-tit"><i></i><span class="tit">筛选</span></div>
      <div class="input-style-form" style="height: 700px; overflow: auto">
        <div class="pop-content" style="">
          <div class="inner-form-area">
            <form>
              <div class="form-item">
                <p class="label-ft">客户编号</p>
                <input
                  type="text"
                  v-model="queryParams.customerId"
                  class="label-input"
                  placeholder="请输入"
                />
              </div>
              <div class="form-item">
                <p class="label-ft">客户名称</p>
                <input
                  type="text"
                  v-model="queryParams.companyName"
                  class="label-input"
                  placeholder="请输入"
                />
              </div>
              <div class="form-item">
                <p class="label-ft">归属人</p>
                <DropGsr @drop-Sec="handleDropSec"></DropGsr>
              </div>
              <div class="form-item">
                <p class="label-ft">客户来源</p>
                <SingleSec
                  :data="dict_datas.customer_source"
                  @selected-tag="handleSelected"
                ></SingleSec>
              </div>
              <div class="form-item">
                <p class="label-ft">客户星级</p>
                <SingleSec
                  :data="rateOptions"
                  @selected-tag="handleSelectedRate"
                ></SingleSec>
              </div>
              <div class="form-item">
                <p class="label-ft">跟进阶段</p>
                <SingleSec
                  :data="dict_datas.follow_up_stage"
                  @selected-tag="handleSelectedFollow"
                ></SingleSec>
              </div>
              <div class="form-item">
                <p class="label-ft">是否有效</p>
                <SingleSec
                  :data="getDictDatas(DICT_TYPE.CUSTOMER_STATUS)"
                  @selected-tag="handleSelectedValid"
                ></SingleSec>
              </div>
              <div class="form-item">
                <p class="label-ft">最近1年是否有订单</p>
                <SingleSec
                  :data="yearValidOptions"
                  @selected-tag="handleSelectedYearValid"
                ></SingleSec>
              </div>
              <div class="form-item">
                <p class="label-ft">历史是否有订单</p>
                <SingleSec
                  :data="statusDictDatas"
                  @selected-tag="handleSelectedOrderStatus"
                ></SingleSec>
              </div>
            </form>
          </div>
        </div>
      </div>
      <div class="btm-area">
        <span class="btn-reset" @click="resetQuery">重置</span>
        <span class="btn-confirm" @click="handleSearch">确定</span>
      </div>
    </van-popup>
    <!-- 排序 -->
    <searchPopup
      v-model="showSearchPop"
      @close-pop="clickOverlay"
      @set-sort="handleSetSort"
    ></searchPopup>
    <!-- 排序 end -->
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { DICT_TYPE, getDictDatas } from "@/utils/dict";
import SearchPopup from "../comp/searchPopup";
import SingleSec from "../comp/singleSec.vue";
import DropGsr from "./comp/dropGsr.vue";
import {
  deleteSaCustomer,
  getSaCustomerPage,
  exportSaCustomerExcel,
  importSaCustomer,
  updateStatus,
  customeTemplate,
  updateAssign,
} from "@/api/member/saCustomer";
const defaultQueryParams = {
  pageNo: 0,
  pageSize: 20,
  isAsc: false,
  columns: "createTime",
  companyName: null,
  customerSource: null,
  applicant: null,
  customerId: null,
  recentYearOrderStatus: false, //最近1年是否有订单
  // province: null,
  // city: null,
  // area: null,
  // address: null,
  // contactName: null,
  // contactMobile: null,
  // contactWx: null,
  // contactTel: null,
  // contactDepart: null,
  // contactPost: null,
  // status: null,
  // remark: null,
  isValid: null,
  // createTime: [],
  // followStatus: null,
  orderStatus: null,
  customerRating: null,
  // followUpStage: null,
};
export default {
  name: "ContractList",
  components: {
    SearchPopup,
    SingleSec,
    DropGsr,
  },
  data() {
    return {
      statusDictDatas: getDictDatas(DICT_TYPE.INFRA_BOOLEAN_STRING),
      yearValidOptions: [
        {
          value: true,
          label: "有",
        },
        {
          value: false,
          label: "无",
        },
      ],
      rateOptions: [
        {
          value: 1,
          label: "1星",
        },
        {
          value: 2,
          label: "2星",
        },
        {
          value: 3,
          label: "3星",
        },
        {
          value: 4,
          label: "4星",
        },
        {
          value: 5,
          label: "5星",
        },
      ],
      showSearchPop: false,
      showSecPop: false,
      loading: false,
      finished: false,
      // 总条数
      total: 0,
      queryParams: {
        ...defaultQueryParams,
      },
      list: [],
    };
  },

  computed: {
    ...mapGetters(["dict_datas", "getDictLabel", "areas", "roles", "userId"]),
  },
  filters: {
    formatFollowUpStage(type) {
      switch (type) {
        case 1:
          return "初识";
        case 2:
          return "意向";
        case 3:
          return "报价";
        case 4:
          return "成交";
        case 5:
          return "搁置";
        default:
          break;
      }
    },
  },
  methods: {
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        ...defaultQueryParams,
      };
      this.queryParams.pageNo = 0;
      this.list = [];
      this.load_more();
      this.showSecPop = false;
    },
    handleSearch() {
      this.list = [];
      this.load_more();
      this.showSecPop = false;
    },
    handleSelectedYearValid(item) {
      this.queryParams.recentYearOrderStatus = item.value;
    },
    handleSelectedOrderStatus(item) {
      this.queryParams.orderStatus = item.value;
    },
    handleDropSec(item) {
      this.queryParams.applicant = item.id;
    },
    handleSelected(item) {
      this.queryParams.customerSource = item.value;
    },
    handleSelectedRate(item) {
      this.queryParams.customerRating = item.value;
    },
    handleSelectedFollow(item) {
      this.queryParams.followUpStage = item.value;
    },
    handleSelectedValid(item) {
      this.queryParams.isValid = item.value;
    },
    handleShowSearch() {
      this.showSearchPop = true;
    },
    clickOverlay() {
      this.showSearchPop = false;
    },
    handleSetSort(sortData) {
      this.queryParams.isAsc = sortData.isAsc;
      this.queryParams.columns = sortData.columns;
      this.list = [];
      this.queryParams.pageNo = 0;
      this.showSearchPop = false;
      this.load_more();
    },
    load_more: function () {
      this.queryParams.pageNo += 1;
      this.onLoad();
    },
    onLoad() {
      this.loading = true;
      let sendData = this.queryParams;
      getSaCustomerPage(sendData).then((res) => {
        let rows = res.data.list;
        if (rows == null || rows.length === 0) {
          this.loading = false; // 关闭加载状态
          this.finished = true; // 加载结束
          return;
        }
        this.loading = false; // 关闭加载状态
        this.total = res.data.total; //数据总条数
        this.list = this.list.concat(rows); //合并数组
        // 如果合并之后的数组长度大于返回的数据总条数
        if (this.list.length >= this.total) {
          this.finished = true; // 加载结束
        }
      });
    },
    handleShowPop() {
      this.showSecPop = true;
    },
    getFirstContactList(row, key) {
      const firstContact =
        row.contactList.find((e) => e.preferredFlag) || row.contactList[0];
      console.log(firstContact);
      if (firstContact) {
        return firstContact[key];
      }
      return "";
    },
    onCancel() {
      this.getList();
    },
    onClickLeft() {
      this.$router.go(-1);
    },
    handleGoDetail(e, item) {
      this.$router.push({
        name: "MCustomerDetail",
        query: {
          id: item.id,
        },
      });
    },
    goDetail(item) {},
    onClickRight() {
      this.$router.push({
        name: "MCustomerAdd",
      });
    },
  },
};
</script>

<style lang="scss">
.contract-list-area {
  .top-fixed {
    position: fixed;
    width: 100%;
    z-index: 555;
  }
  .op-area {
    display: flex;
    justify-content: space-between;
    padding: 14px 16px;
    background-color: #fff;
    .op-left {
      .iconfont {
        color: #999;
        margin: 0 0 0 2px;
      }
    }
  }
  .list-contract {
    padding: 106px 0 0 0;
    li {
      padding: 0 12px;
      margin: 0 0 12px 0;
    }
  }
  .list-content-box {
    background-color: #fff;
    border-radius: 6px 6px 6px 6px;
    .hd {
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 12px 12px 12px 0px;
    }
    .hd-lf {
      flex: 1;
    }
    .bd {
      .img-area {
        text-align: center;
      }
      .inner-bd {
        padding: 12px;
        display: flex;
        justify-content: center;
        align-items: center;
        .bd-lf {
          flex: 1;
        }
        .bd-right {
          text-align: center;
        }
      }
      .row-item {
        margin: 0 0 8px 0;
      }
    }
    .bottom {
      overflow: hidden;
      padding: 9px 12px;
      .list-inner-inf {
        display: flex;
        width: 230px;
        margin: -16px 0 0 0;
        span {
          flex: 1;
          display: block;
          text-align: center;
        }
      }

      .b-l2 {
        display: inline-block;
        // vertical-align: middle;
      }
      .b-l1 {
        width: 80px;
        display: inline-block;
        margin: 0 12px 0 0;
      }
    }
    .hd-bor {
      width: 3px;
      margin: 0 12px 0 0;
      height: 15px;
      background: #3875f6;
      border-radius: 0px 2px 2px 0px;
      display: inline-block;
      vertical-align: middle;
    }
    h2 {
      font-size: 14px;
      color: #222222;
      font-weight: 700;
      display: inline-block;
      vertical-align: middle;
      margin: 0;
      padding: 0;
    }
    .hd-rt {
      .doing {
        font-size: 12px;
        color: #3875f6;
      }
    }
  }

  .btm-area {
    position: fixed;
    width: 100%;
    bottom: 0;
    left: 0;
    border-top: 1px solid #efefef;
    display: flex;
    height: 49px;
    line-height: 49px;
    background: #ffffff;
    .btn-reset {
      text-align: center;
      flex: 1;
      font-size: 16px;
      color: #3875f6;
      cursor: pointer;
    }
    .btn-confirm {
      text-align: center;
      font-size: 16px;
      color: #fff;
      background: #3875f6;
      flex: 1;
      cursor: pointer;
    }
  }
  .pop-tit {
    // position: fixed;
    width: 100%;
    background-color: #fff;
    padding: 22px 12px;
    height: 50px;
    overflow: hidden;
    border-radius: 8px;
    i {
      width: 3px;
      height: 15px;
      background: #3875f6;
      border-radius: 2px 2px 2px 2px;
      display: inline-block;
      vertical-align: middle;
      margin: 0 10px 0 0;
    }
    .tit {
      font-weight: 700;
      font-size: 16px;
      color: #222222;
      line-height: 22px;
      text-align: left;
      display: inline-block;
      vertical-align: middle;
    }
  }
  .input-style-form {
    padding: 0px 12px 22px 12px;

    .inner-form-area {
      padding: 16px 0 0 8px;
    }
    .form-item {
      margin: 0 0 20px 0;
      .label-ft {
        font-size: 14px;
        color: #666666;
        line-height: 20px;
        text-align: left;
        margin: 0 0 4px 0;
      }
      .label-input {
        width: 327px;
        height: 44px;
        background: #ffffff;
        border-radius: 4px 4px 4px 4px;
        border: 1px solid #efefef;
        font-size: 14px;
        padding: 0 6px;
        overflow: hidden;
      }
      .date-picker {
        display: inline-block;
        position: relative;
        .label-input {
          width: 150px;
          height: 44px;
          background: #ffffff;
          border-radius: 4px 4px 4px 4px;
          border: 1px solid #efefef;
        }
        .icon-date {
          position: absolute;
          right: 18px;
          top: 14px;
          display: inline-block;
          img {
            width: 14px;
            height: auto;
          }
        }
      }
    }
  }
  .item-sec-taglis {
    li {
      float: left;
      margin: 0 10px 10px 0;
    }
    .inner-tag-item {
      width: 101px;
      height: 38px;
      background: #f6f6f6;
      font-size: 14px;
      color: #222222;
      line-height: 38px;
      text-align: center;
      border-radius: 4px 4px 4px 4px;
      border: 1px solid #efefef;
      overflow: hidden;
    }
    .tag-item-active {
      display: inline-block;
      position: relative;
      .inner-tag-item {
        width: 101px;
        height: 38px;
        background: #f7faff;
        border-radius: 4px 4px 4px 4px;
        border: 1px solid #3875f6;
        color: #3875f6;
      }
      .tangle {
        width: 18px;
        height: 16px;
        background: #3875f6;
        border-radius: 8px 0px 4px 0px;
        border: 1px solid #3875f6;
        position: absolute;
        right: 0px;
        bottom: 0px;
        text-align: center;
        .van-icon-success {
          margin: 2px 0 0 0;
        }
      }
    }
  }
}
</style>
